//
//  PerformanceOptiomizationViewController.m
//  DebugDemo
//
//  Created by XFB on 2019/9/9.
//  Copyright © 2019 ZhouCheng. All rights reserved.
//

#import "PerformanceOptiomizationViewController.h"

@interface PerformanceOptiomizationViewController ()

@property (nonatomic, weak) UIImageView *imageView;

@end

@implementation PerformanceOptiomizationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];

    [self image];
}

- (void)image
{
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.frame = CGRectMake(20, 100, ScreenW-40, 200);
//    imageView.image = [UIImage imageNamed:@"James"];
    [self.view addSubview:imageView];
    self.imageView = imageView;
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
       
        /// 获取CGImage
        CGImageRef cgImage = [UIImage imageNamed:@"James"].CGImage;
        
        /// alphaInfo
        CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(cgImage) & kCGBitmapAlphaInfoMask;
        
        BOOL hasAlpha = NO;
        if (alphaInfo == kCGImageAlphaPremultipliedLast ||
            alphaInfo == kCGImageAlphaPremultipliedFirst ||
            alphaInfo == kCGImageAlphaLast ||
            alphaInfo == kCGImageAlphaFirst) {
            hasAlpha = YES;
        }
        
        /// bitmapInfo
        CGBitmapInfo bitmapInfo = kCGBitmapByteOrder32Host;
        bitmapInfo |= hasAlpha? kCGImageAlphaPremultipliedFirst : kCGImageAlphaNoneSkipFirst;
        
        /// size
        size_t width = CGImageGetWidth(cgImage);
        size_t height = CGImageGetHeight(cgImage);
        
        /// context
        CGContextRef context = CGBitmapContextCreate(NULL, width, height, 8, 0, CGColorSpaceCreateDeviceRGB(), bitmapInfo);
        
        /// draw
        CGContextDrawImage(context, CGRectMake(0, 0, width, height), cgImage);
        
        /// get CGImage
        cgImage = CGBitmapContextCreateImage(context);
        
        /// into UIImage
        UIImage *newImage = [UIImage imageWithCGImage:cgImage];
        
        /// release
        CGContextRelease(context);
        CGImageRelease(cgImage);
        
        /// 回到主线程刷新UI
        dispatch_async(dispatch_get_main_queue(), ^{
            self.imageView.image = newImage;
        });
        
    });
    
    
}



@end
